var express = require('express');
const mailModel = require('../Model/mailModel')
const LikeModel = require('../Model/LikeModel')
const collectModel = require('../Model/collectModel')
const materiaModel = require('../Model/materiaModel')
const token = require('../utils/token')
var router = express.Router();

/* 用户登录判断 */
router.post('/userLogin', token, async (req, res, next) => {
  const { _id } = req.body
  const data = await mailModel.findOne({ _id })
  res.json({ code: '200', data })
});

//用户收藏素材
router.post('/collect', token, async (req, res, next) => {
  const { mail, id } = req.body
  console.log(mail, id);
  const data = await collectModel.findOne({ mail, Material: id })
  if (data) {
    await collectModel.deleteOne({ mail, Material: id })
    res.json({ code: '201' })
  } else {
    await collectModel.create({ mail, Material: id })
    res.json({ code: '200' })
  }
})

//初始化收藏素材
router.post('/collectinitialization', async (req, res, next) => {
  const { mail, id } = req.body
  const data = await collectModel.findOne({ mail, Material: id })
  if (data) return res.json({ code: '200' })
  res.json({ code: '201' })
})
//添加删除喜爱素材
router.post('/Live', token, async (req, res, next) => {
  const { mail, id } = req.body
  const data = await LikeModel.findOne({ mail, Material: id })
  if (data) {
    await LikeModel.deleteOne({ mail, Material: id })
    res.json({ code: '201' })
  } else {
    await LikeModel.create({ mail, Material: id })
    res.json({ code: '200' })
  }
})

//初始化喜爱素材
router.post('/Liveinitialization', async (req, res, next) => {
  const { mail, id } = req.body
  const data = await LikeModel.findOne({ mail, Material: id })
  if (data) return res.json({ code: '200' })
  res.json({ code: '201' })
})

//用户的所有收藏
router.post('/userCollect', token, async (req, res, next) => {
  const { mail } = req.body
  const data = await collectModel.find({ mail })
  res.json({ code: '200', data })
})

//用户的所有喜爱
router.post('/userLike', token, async (req, res, next) => {
  const { mail } = req.body
  const data = await LikeModel.find({ mail })
  res.json({ code: '200', data })
})

//用户的所有喜爱数据
router.post('/userLikeData', async (req, res, next) => {
  const { mail } = req.body
  const arr = []
  const data = await LikeModel.find({ mail })
  for (const item of data) {
    const newData = await materiaModel.findOne({ _id: item.Material })
    arr.push(newData)
  }
  res.json({ code: '200', data: arr })
})

//用户的所有收藏数据
router.post('/userCollectData', async (req, res, next) => {
  const { mail } = req.body
  const arr = []
  const data = await collectModel.find({ mail })
  for (const item of data) {
    const newData = await materiaModel.findOne({ _id: item.Material })
    arr.push(newData)
  }
  res.json({ code: '200', data: arr })
})

//用户数据
router.post('/userData', async (req, res, next) => {
  const data = await mailModel.find()
  res.json({ code: '200', data })
})

//添加次数
router.post('/UpUserData', async (req, res, next) => {
  const { num, id } = req.body
  await mailModel.updateOne({_id:id},{ frequency: num })
  const data = await mailModel.find()
  res.json({ code: '200', data })
})

//封禁账户
router.post('/BanUserData', async (req, res, next) => {
  const { id } = req.body
  await mailModel.updateOne({_id:id},{ state: '封禁' })
  const data = await mailModel.find()
  res.json({ code: '200', data })
})

//解封账户
router.post('/ReleaseUserData', async (req, res, next) => {
  const { id } = req.body
  await mailModel.updateOne({_id:id},{ state: '正常' })
  const data = await mailModel.find()
  res.json({ code: '200', data })
})

//搜索用户
router.post('/user', async (req, res, next) => {
  const { id } = req.body
  await mailModel.updateOne({_id:id},{ state: '正常' })
  const data = await mailModel.find()
  res.json({ code: '200', data })
})

module.exports = router;
